var date = new Date();
date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
	
var queryMine = false;
var queryGaven = false;
var queryBy = false;

var taskid = null;

$(document).ready(function() {
	
	$.ajax({
				type : "post",
				url : "lzw/taskTrack_queryMyTasks.action",
				data : getParams(1),
				dataType : "JSON",
				success : showPage,
				error : function(textStatus) {
					alert("Ajax请求失败，请检查本地的网络连接！");
					return false;
				}
			});
			
	// 分页查询，异步提交
	function doAjax(d) {
		$.ajax({
					type : "post",
					url : "lzw/taskTrack_queryMyTasks.action",
					data : getParams(d),
					dataType : "JSON",
					success : showPage,
					error : function(textStatus) {
						alert("Ajax请求失败，请检查本地的网络连接！");
						return false;
					}
				});

	}

	//传入页数
	function getParams(p) {
		var Deptid = "null";
		var Eid = "null";
		if(queryBy){
			Deptid = $("#DId").attr("value");
			if(Deptid==""){
				Deptid = "null";
			}
		    Eid = $("#EId").attr("value");
		    if(Eid==""){
		    	Eid = "null";
		    }
		} 
		var page = p;
        var param = {
        	"queryOption['page']":page,
        	"queryOption['queryMine']":queryMine,
        	"queryOption['queryGaven']":queryGaven,
        	"queryOption['querybyDept']":Deptid,
        	"queryOption['queryByEid']":Eid
        };
		$("#taskList").empty();
		$("#loadingTask").show();
		return param;
	}

	$("#mytask").click(function(){
		if($("#givetask").attr("checked")==true){
			$("#givetask").attr("checked",false);
		}
		queryGaven = false;
		queryMine = true;
		queryBy = false;
		doAjax(1);
	});

	$("#givetask").click(function(){
		if($("#mytask").attr("checked")==true){
			$("#mytask").attr("checked",false);
		}
		queryMine = false;
		queryGaven = true;
		queryBy = false;
		doAjax(1);
	});
	
	$("#query").click(function(){
        queryBy = true;
		doAjax(1);
	});
		
	$("#reset").click(function(){
		queryMine = false;
		queryGaven = false;
		queryBy = false;
		$("#DId").attr("value","");
		$("#EId").attr("value","");
		doAjax(1);
	});
	
	// 分页查询，查第一页
	$("#firstPage").click(function() {
				doAjax(1);
			});
	// 分页查询，查上一页
	$("#prePage").click(function() {
				var pageCount = $("#pageCount").text();
				var page = $("#page").text();
				page = (parseInt(page) - 1);
				if (page <= 0) {
					alert("当前已是第一页！");
					return false;
				}
				doAjax(page);
			});
	// 分页查询，查下一页
	$("#nextPage").click(function() {
				var pageCount = $("#pageCount").text();
				var page = $("#page").text();
				page = (parseInt(page) + 1);
				if (page > pageCount) {
					alert("当前已是末页！");
					return false;
				}
				doAjax(page);
			});
	// 分页查询，查最后一页
	$("#lastPage").click(function() {
				var page = $("#pageCount").text();
				doAjax(page);
			});
	// 分页查询，跳转到第n页
	$("#jumpPage").click(function() {
				var pageCount = $("#pageCount").text();
				var page = $("#jump").val();
				if (page > pageCount) {
					alert("页数越界！");
					$("#pageCount").text(0);
					return false;
				}
				doAjax(page);
			});

	function showPage(data) {

		$("#taskList").empty();
		var d = eval("(" + data + ")");
		$("#leader").text(d.leader);
		if (d.userTasks == null && d.errMsg != null) {
			$("#loadingTask").hide();
			$("#noneTask").show();
			$("#total").text(0);
			$("#page").text(0);
			$("#pageCount").text(0);
		} else {
			$("#taskList").hide();
			$("#noneTask").hide();
			$("#loadingTask").hide();

			$.each(d.userTasks.list, function(i, n) {
						fillDataToHtml(n.EId, n.id, n.name, n.time, n.uname, n.dept,
								n.phone, n.status, n.sid, n.leadname, n.leadphone, n.leadDepts,
								n.newFeedBack, n.newOverReq);
					});

					
		    $("#total").text(d.userTasks.total);
		    $("#page").text(d.userTasks.page);
		    $("#pageCount").text(Math.ceil(d.userTasks.total / 30));
			$("#taskList").show();
		}

	}

	function fillDataToHtml(EId, id, name, time, uname, dept, phone, status, sid, lname, lphone, lDepts, newfb, newor) {

		var strHtml = "<tr>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ "<input name='button' type='button' value='查看'id='"
				+ $.trim(id) // 去除id字符串的空格
				+ "'/></div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF' class='eid'><div align='center' class='STYLE1'>"
				+ EId + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ id + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ name + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ time + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1' id="+sid+">"
				+ status + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
		        +"<a class='"+$.trim(id)+"' name='"+$.trim(id)+"' href='javascript:;'>"+newfb+"</a>"
				+"</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
		        +"<a class='"+$.trim(EId)+$.trim(id)+"' name='"+$.trim(id)+"' href='javascript:;'>"+newor+"</a>"
		        +"</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ lDepts + "/ "+ lname + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ lphone + "</div></td>";		
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ dept+"/ "+uname + "</div></td>";
		strHtml += "<td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>"
				+ phone + "</div></td>";
		strHtml += "</tr>";
		$("#taskList").append(strHtml);
		
		var fbid = "." + $.trim(id);
		$(fbid).click(function(){
			$.cookie('tasktrackid',null);
			$.cookie('tasktrackid', $(this).attr("name"), { path: '/', expires: date });
			$('#tabFrame', window.parent.document).attr("src", "/ag/lzw/tracklist_trackdetail_main.jsp");
		});
		
		var orid = "." + $.trim(EId) + $.trim(id);
		$(orid).click(function(){
			taskid = $(this).attr("name");
			var eid = $(this).parent().parent().parent().children(".eid").children(".STYLE1").text();
//			alert(eid);
			$.ajax({
					type : "post",
					url : "lzw/taskTrack_isLeader.action?eid="+eid,
					dataType : "JSON",
					success : function(data){
				    var data = eval("(" + data + ")");
//				    alert(data.leader);
				    if(data.leader=="1"){
		              $.cookie('zhhtaskid',null);
		              $.cookie('zhhtaskid', taskid, { path: '/', expires: date });
//			          $.cookie('zhhtaskid', $(this).attr("name"), { path: '/', expires: date });
			          $('#tabFrame', window.parent.document).attr("src", "/ag/zhh/taskOver_request_main.jsp");
		             }else{
			           $.cookie('tasktrackid',null);
			           $.cookie('tasktrackid', taskid, { path: '/', expires: date });
//			           $.cookie('tasktrackid', $(this).attr("name"), { path: '/', expires: date });
			           $('#tabFrame', window.parent.document).attr("src", "/ag/lzw/taskOver_request_main.jsp");
			          }
					},
					error : function(textStatus) {					
						return false;
					}
				});
		});
		
		
		var id_value = "#" + $.trim(id);// 去除id字符串的空格
		$(id_value).click(function() {
			var date = new Date();
	        date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
			//$.cookie('tasktrackid', $(this).attr("id"));
	        $.cookie('tasktrackid',null);
			$.cookie('tasktrackid', $(this).attr("id"), { path: '/', expires: date });
			$.cookie('taskEventId', EId);
			var statusId = $(this).parent().parent().next().next().next().next().next().children("div").attr("id")
			if(statusId=='1000'){
				window.location.href = "/ag/lzw/task_accept.jsp";
			} else {
				window.location.href = "/ag/lzw/task_main.jsp";
			}
			// $('#tabFrame', window.parent.document).attr("src","lzw/task_main.jsp");
			});
			
			
	}
});
